const download = require('download-git-repo')
const ora = require('ora')

const BASE_URL = `https://gitee.com/yqw19920625`;

// const toUnixPath = () => {
//     let p = process.env[process.platform === 'win32' ? 'USERPROFILE' : 'HOME'];
//     p.replace(/\\/g, '/')
//     return path.resolve(p, '.tmp');
// }

const downloadCode = (project, dir) => {
    const spinner = ora('下载中。。。').start();
    return new Promise((resolve, reject) => {
        download(`direct:${BASE_URL}/${project}-stage.git`, `${dir}/${project}`, { clone: true }, (err) => {
            if (err) {
                spinner.fail('下载失败！请重新下载。')
                reject({ state: 'failed', message: err })
            } else {
                spinner.succeed('下载成功！')
                resolve({ state: 'success' });
            }
        })
    })
}

module.exports = downloadCode;